#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<netdb.h>
static char c[1000];
#define STRDUP_ALLOCA(ptr,str) do{              \
    (ptr) = (char *)alloca(strlen(str) + 1);    \
    strcpy(ptr,str);                            \
  }while(0)

int select_fd(int fd,int maxtime,int writep)
{
  fd_set fds,exeptfds;
  struct timeval timeout;
  FD_ZERO(&fds);
  FD_SET(fd,&fds);
  FD_ZERO(&exeptfds);
  FD_SET(fd,&exeptfds);
  timeout.tv_sec = maxtime;
  timeout.tv_usec = 0;
  return select(fd + 1,writep?NULL:&fds,writep?&fds:NULL,
                &exeptfds,&timeout);
}

int iwrite(int fd,char *buf,int len)
{
  int res = 0;
  while(len>0)
    {
      do{
        if(900)
          {
            do
              {
                res = select_fd(fd,2,1);
              }
            while(res ==  -1);
            if(res <= 0)
              {
                if(res == 0)
                  {//I hope It could do something here;
                  }
                return  -1;
              }
          }
        res = write(fd,buf,len);
      }
      while(res ==  -1);
      if(res <= 0)
        break;
      buf += res;
      len = len - res;
    }
  return res;
}

int iread(int fd,char *buf,int len)
{
  int errno;
  int res;
  do{
    if(900)
      {
        do
          {
            res = select_fd (fd, 90 , 0);
          }
        while(res == -1);
          if(res <= 0)
            {
              if(res == 0)
                errno = 0;
              return  -1;
            }
      }
    res = recv (fd, buf, len,0);
  }
  while(res ==  -1);
  
  return res;
}

int sendRequest(int fd,char *request)
{
  iwrite(fd,request,strlen(request));
  memset(c,'\0',sizeof(c));
  iread(fd,c,sizeof(c));
  printf("****************************************\n");
  printf("%s",c);
  printf("****************************************\n");
  return 0;
}

int main(int argc, char *argv[])
{
  const char *hostname = "postoffice.uts.edu.au";
  //  const char *command = "GET";
  //  const char *path = "/";
  //  const char *useragent = "diwu";
  //  const char *useragent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
  int hostport = 25;
  struct hostent *hp;
  struct in_addr in;
  struct sockaddr_in sock_name;
  char *request = (char *) malloc(200);
  memset(request,'\0',200);

  int int_for_sock = 0;
  int *sock = &int_for_sock;
  char *inet_s;


  hp = gethostbyname(hostname);
  unsigned long int addr;
  addr = inet_addr(hostname);


  memcpy(&sock_name.sin_addr,hp -> h_addr_list[0],hp -> h_length);
  memcpy(&in,*hp -> h_addr_list,sizeof(in));
  sock_name.sin_family = AF_INET;
  sock_name.sin_port = htons(hostport);
  STRDUP_ALLOCA(inet_s,inet_ntoa(in));
  if((*sock = socket(AF_INET,SOCK_STREAM,0)) ==  -1)
    return 1;

  if(connect(*sock,(struct sockaddr *)&sock_name,sizeof(sock_name)))
    return 1;

  int res = 1;
  request = "helo 1\r\n";
  sendRequest(*sock,request);
  request = "mail from: <hacker@someplace.com>\r\n";
  sendRequest(*sock,request);
  request = "rcpt to: <Di.Wu-4@student.uts.edu.au>\r\n";
  sendRequest(*sock,request);
  request = "DATA\r\n";
  sendRequest(*sock,request);
  request = "Subject:you die\r\nSend the c prgramme from diwu\r\n";
  sendRequest(*sock,request);
  request = ".\r\n";
  sendRequest(*sock,request);
  request = "QUIT\r\n";
  sendRequest(*sock,request);
  
  close(*sock);
  return 0;
}
